<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket web client</title>
</head>

<body>
    <h1>WebSocket Web Client</h1>
    <br />

    <input type="text" placeholder="enter your message" id="message">
    <button id="sendBtn">Send</button>
    <ul id="messageList"></ul>

    <script>
        // 根据实际地址和端口进行修改，其他内容无需修改
        const uri = "ws://localhost:5000/ws";
        socket = new WebSocket(uri);
        socket.onopen = function (e) {
            console.log("websocket estabished!");
        }

        socket.onclose = function (e) {
            console.log('websocket closed!');
        }

        socket.onmessage = function (e) {
            appendItem(list, e.data);
            console.log(e.data);
        }

        const list = document.getElementById("messageList");
        const btn = document.getElementById("sendBtn");
        btn.addEventListener("click", function () {
            console.log("sending message~~~");

            var messgae = document.getElementById("message");
            socket.send(message.value)
        })

        function appendItem(list, message) {
            const li = document.createElement("li");
            li.appendChild(document.createTextNode(message));
            list.appendChild(li);
        }
    </script>
</body>

</html>

